/**
 * Copyright 2012 markiewb
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package de.markiewb.netbeans.plugin.showpathintitle.options;

import de.markiewb.netbeans.plugin.showpathintitle.ShowPathInTitleOptions;
import java.util.prefs.Preferences;
import org.openide.util.NbPreferences;

final class ShowpathintitlePanel extends javax.swing.JPanel {

    private final ShowpathintitleOptionsPanelController controller;
    private Preferences pref = NbPreferences.forModule(ShowPathInTitleOptions.class);
    private ShowPathInTitleOptions options;

    ShowpathintitlePanel(ShowpathintitleOptionsPanelController controller) {
        this.controller = controller;
        initComponents();
        // TODO listen to changes in form fields and call controller.changed()
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        txtIntroduction = new javax.swing.JLabel();
        cbProjectName = new javax.swing.JCheckBox();
        cbPath = new javax.swing.JCheckBox();
        cbVersion = new javax.swing.JCheckBox();
        btnReset = new javax.swing.JButton();
        cbRelativePath = new javax.swing.JCheckBox();
        rbUseEditorAsReference = new javax.swing.JRadioButton();
        rbUseNodeAsReference = new javax.swing.JRadioButton();
        cbProjectGroup = new javax.swing.JCheckBox();

        org.openide.awt.Mnemonics.setLocalizedText(txtIntroduction, org.openide.util.NbBundle.getMessage(ShowpathintitlePanel.class, "ShowpathintitlePanel.txtIntroduction.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(cbProjectName, org.openide.util.NbBundle.getMessage(ShowpathintitlePanel.class, "ShowpathintitlePanel.cbProjectName.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(cbPath, org.openide.util.NbBundle.getMessage(ShowpathintitlePanel.class, "ShowpathintitlePanel.cbPath.text")); // NOI18N
        cbPath.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                cbPathStateChanged(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(cbVersion, org.openide.util.NbBundle.getMessage(ShowpathintitlePanel.class, "ShowpathintitlePanel.cbVersion.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(btnReset, org.openide.util.NbBundle.getMessage(ShowpathintitlePanel.class, "ShowpathintitlePanel.btnReset.text")); // NOI18N
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(cbRelativePath, org.openide.util.NbBundle.getMessage(ShowpathintitlePanel.class, "ShowpathintitlePanel.cbRelativePath.text")); // NOI18N

        buttonGroup1.add(rbUseEditorAsReference);
        org.openide.awt.Mnemonics.setLocalizedText(rbUseEditorAsReference, org.openide.util.NbBundle.getMessage(ShowpathintitlePanel.class, "ShowpathintitlePanel.rbUseEditorAsReference.text")); // NOI18N
        rbUseEditorAsReference.setToolTipText(org.openide.util.NbBundle.getMessage(ShowpathintitlePanel.class, "ShowpathintitlePanel.rbUseEditorAsReference.toolTipText")); // NOI18N

        buttonGroup1.add(rbUseNodeAsReference);
        org.openide.awt.Mnemonics.setLocalizedText(rbUseNodeAsReference, org.openide.util.NbBundle.getMessage(ShowpathintitlePanel.class, "ShowpathintitlePanel.rbUseNodeAsReference.text")); // NOI18N
        rbUseNodeAsReference.setToolTipText(org.openide.util.NbBundle.getMessage(ShowpathintitlePanel.class, "ShowpathintitlePanel.rbUseNodeAsReference.toolTipText")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(cbProjectGroup, org.openide.util.NbBundle.getMessage(ShowpathintitlePanel.class, "ShowpathintitlePanel.cbProjectGroup.text")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnReset))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cbProjectGroup)
                            .addComponent(cbProjectName)
                            .addComponent(cbPath)
                            .addComponent(cbVersion)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(21, 21, 21)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(rbUseNodeAsReference)
                                    .addComponent(cbRelativePath)
                                    .addComponent(rbUseEditorAsReference)))
                            .addComponent(txtIntroduction))
                        .addGap(0, 31, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtIntroduction)
                        .addGap(11, 11, 11)
                        .addComponent(cbProjectGroup)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbProjectName)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbPath)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbRelativePath)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(rbUseNodeAsReference)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(rbUseEditorAsReference)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbVersion)
                        .addGap(0, 54, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnReset)))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
        options.reset();
        initUIFromOptions();
    }//GEN-LAST:event_btnResetActionPerformed

    private void cbPathStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_cbPathStateChanged
        boolean selected = cbPath.isSelected();

        //enable or disable dependend widgets
        cbRelativePath.setEnabled(selected);
        rbUseNodeAsReference.setEnabled(selected);
        rbUseEditorAsReference.setEnabled(selected);
    }//GEN-LAST:event_cbPathStateChanged

    void load() {
        options = ShowPathInTitleOptions.loadFrom(pref);
        initUIFromOptions();
    }

    void store() {
        options.showProjectGroup= cbProjectGroup.isSelected();
        options.showProjectName = cbProjectName.isSelected();
        options.showFileName = cbPath.isSelected();
        options.showIDEVersion = cbVersion.isSelected();
        options.showRelativeFilename = cbRelativePath.isSelected();
        options.useNodeAsReference = rbUseNodeAsReference.isSelected();
        options.useEditorAsReference = rbUseEditorAsReference.isSelected();
        options.saveTo(pref);
    }

    boolean valid() {
        // TODO check whether form is consistent and complete
        return true;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnReset;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JCheckBox cbPath;
    private javax.swing.JCheckBox cbProjectGroup;
    private javax.swing.JCheckBox cbProjectName;
    private javax.swing.JCheckBox cbRelativePath;
    private javax.swing.JCheckBox cbVersion;
    private javax.swing.JRadioButton rbUseEditorAsReference;
    private javax.swing.JRadioButton rbUseNodeAsReference;
    private javax.swing.JLabel txtIntroduction;
    // End of variables declaration//GEN-END:variables

    private void initUIFromOptions() {
        cbProjectGroup.setSelected(options.showProjectGroup);
        cbVersion.setSelected(options.showIDEVersion);
        cbProjectName.setSelected(options.showProjectName);
        cbPath.setSelected(options.showFileName);
        cbRelativePath.setSelected(options.showRelativeFilename);
        rbUseNodeAsReference.setSelected(options.useNodeAsReference);
        rbUseEditorAsReference.setSelected(options.useEditorAsReference);
    }
}
